'''
Created on 2010/07/07

@author: saburo
'''
import serial
import abc
from pyrfid.DataProtocol.DSFID.DSFID import DSFID

class DriverInterface(object):
#    __metaclass__ = abc.ABCMeta
    _ser = serial.Serial()
    isBlockWrite = False
    dsfid_on_hex = ""
    binary = "8108487EB32D8307733F499F5801231E240070DE0000000000000000000000000000000000000000000000"
    
    HF_VIRTUAL_TAG_TYPE = 0
    UHF_VIRTUAL_TAG_TYPE = 1
    
    @staticmethod
    def generateDriver(driverType, isMultipleWordAccess = False):
        driver = object()
#        if driverType == 'JTEUSBDriver':
#            driver = JTEUSBDriver(isMultipleWordAccess)
#        if driverType == 'JTEDriver':
#            driver = JTEDriver(isMultipleWordAccess)
#        elif driverType == 'HitachiMediumDriver':
#            driver =  HitachiMediumDriver(isMultipleWordAccess)
#        driver.isBlockWrite = isMultipleWordAccess
        return driver
    
    def connectRW(self, port = "", baudrate = 38400, bytesize = 8, parity = "N", stopbits = 1):
        print "connectRW"
        try:
            self._ser.port = port
            self._ser.baudrate = baudrate
            self._ser.bytesize = bytesize
            self._ser.stopbits = stopbits
            self._ser.open()
        except serial.serialutil.SerialException:
            print "serial port could not open"
        
        return self._ser.isOpen()
        
    def disconnectRW(self):
        #print "HITACHI DRIVER turnOff......"
        if self._ser.isOpen() == True:
            #print "TURN OFF...."
            self._ser.close()
    
    def getDSFID(self, tag_id):
        
        address = 0
        read_data =  self.readData(tag_id, address, 1)
        if read_data == "00":
            self.dsfid_on_hex = read_data
            return read_data
        
        while DSFID.isDSFID(read_data) != True:
            address += 1
            read_data +=  self.readData(tag_id, address, 1)
        
        self.dsfid_on_hex = read_data 
        return read_data
    
    @abc.abstractmethod
    def writeData(self, tagId, startAddress, data):
        """return isWrited"""
        pass
#        header_binary = self.binary[:startAddress * 2]
#        self.binary = header_binary + data + self.binary[startAddress * 2 + len(data):]
#        print "LOGICAL MEMORY====================================================================="   
#        print self.binary
#        print "DSFID ON MEMORY====================================================================="   
#        print self.dsfid_on_hex        
    
    @abc.abstractmethod
    def readData(self, tagId, startAddress, length):
        """return readDataonHex"""
        pass
        #for ????        
#        binary = "00C97B06089D8080804F8232070420F1CB3D35DA1D013211060B3A73CE2FF20201F8430A38CB71CB3D35DB7E39C2440538CB79E798150204C01605024CB016EA17067048860DDF7900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000371730162C1526441A4317020E110B1D0032824F"        
        #for Packed Objects        
#        for Non-Directory
#        binary = "0428FC5909004F8386331654EC34CF3CB5DF1C4D238C33C72C30C30C30C30C30C600"
#        return self.binary[startAddress*2:(startAddress*2 )+ (length*2)]
    
    def getLockSize(self):
        return 8
    
    @abc.abstractmethod
    def configureAFI(self, tagId, afi):
        """return isConfiguredAFI"""
        pass
        
    @abc.abstractmethod
    def configureDSFID(self, tagId, dsfid):
        """return isConfiguredDSFID"""
        self.binary = dsfid + self.binary[len(dsfid):]
        self.dsfid_on_hex = dsfid
        
    @abc.abstractmethod
    def inventoryTags(self):
        """return TIDArrays"""
        pass
    